<template>

<div>
  <el-menu
    :default-active="activeIndex2"
    class="el-menu-demo"
    mode="horizontal"
    @select="handleSelect"
    background-color="#E4E7ED"
    text-color="#303133"
    active-text-color="#3A76BF">
    <el-menu-item index="1" class="mlogo">欢迎来到此的博客!</el-menu-item>
    <el-menu-item index="2"><el-avatar :size="50" :src="user.avatar"></el-avatar>
              {{ user.username }}</el-menu-item>
    <el-menu-item index="3"><el-link href="/blogs">主页</el-link></el-menu-item>
      <el-menu-item index="4"><el-link href="/blog/add">发表博客</el-link></el-menu-item>
      <el-menu-item index="5" v-show="!hasLogin"><el-link href="/login">登录</el-link></el-menu-item>
      <el-menu-item index="6" v-show="hasLogin"><el-link @click="logout">退出</el-link></el-menu-item>
  </el-menu>
  </div>
</template>

<script>
  export default {
    name: "Header",
    data() {
      return {
        user: {
          username: '请先登录',
          avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
        },
        hasLogin: false
      }
    },
    methods: {
      logout() {
        const _this = this
        _this.$axios.get("/logout", {
          headers: {
            "Authorization": localStorage.getItem("token")
          }
        }).then(res => {
          _this.$store.commit("REMOVE_INFO")
          _this.$router.push("/login")

        })
      }
    },
    created() {
      if(this.$store.getters.getUser.username) {
        this.user.username = this.$store.getters.getUser.username
        this.user.avatar = this.$store.getters.getUser.avatar

        this.hasLogin = true
      }

    }
  }
</script>

<style scoped>

 .el-menu {
  height: 80px;
}
.el-menu-item {
  font-size: 18px;
}
.el-menu-item .el-link {font-size: 18px; color: #000000;}

.mlogo{
    padding-left: 10%;
    font-weight: 400;
    color: #000000;
    font-size: 24px;
}

.el-menu-demo:before {
    background: Transparent;
    content: "";
    height: 6px;
    width: 100%;
    transition: ease-in-out .5s;
}
</style>
